package com.dmai.capacity.platform.user.controller;

import com.dmai.capacity.platform.common.context.TenantContextHolder;
import com.dmai.capacity.platform.common.dto.ResponseEntity;
import com.dmai.capacity.platform.constants.InstitutionAuthStateEnum;
import com.dmai.capacity.platform.user.pojo.request.RefundEmailRequest;
import com.dmai.capacity.platform.user.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletRequest;

/**
 * 发邮件
 */
@Slf4j
@RestController
public class SysEmailController {

    @Autowired
    private IEmailService emailService;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 发送退费审核结果通知邮件
     */
    @PostMapping("/mails/sendRefund")
    public ResponseEntity sendRefundEmail(@RequestBody RefundEmailRequest target, HttpServletRequest request) {
        log.info("开始发送退费审核通知邮件：{}...", target.getReceiver());
        Context context = new Context();
        context.setVariable("authResult", target.getAuthState().equals(InstitutionAuthStateEnum.PASS.getCode()) ? "已经通过审核" : "被驳回");
        String content = templateEngine.process("mail_refund_template.html", context);
        emailService.sendHtml(TenantContextHolder.getTenant(), target.getReceiver(), "【考级考务管理系统】审核结果通知", content);
        log.info("发送退费审核通知邮件成功...");

        return ResponseEntity.succeed("OK");
    }

}
